print(Sys.time())

## write in replication material directory
## setwd("../")

options(stringsAsFactors=F)

library(checkpoint)
## this should install all of the packages needed for the analyses
## except parrot (install with command below)
checkpoint("2018-12-01")

## I recommend installing Microsoft R Open to use checkpoint
## (it will be installed as part of MRO)
## if checkpoint does not work for you (it can be buggy, esp. outside MRO),
## install packages with this command:
## install.packages(c(
##     "Matrix",
##     "ROCR",
##     "RSpectra",
##     "caret",
##     "e1071",
##     "devtools",
##     "dplyr",
##     "ggplot2",
##     "glmnet",
##     "igraph",
##     "irr",
##     "parallel",
##     "plyr",
##     "pryr",
##     "randomForest",
##     "readr",
##     "stm",
##     "stringi",
##     "text2vec",
##     "tidyr",
##     "tidytext",
##     "tidyverse",
##     "tm",
##     "xtable",
##     "zoo",
##     "textdata",
##     "stargazer"
##     ))
## and run get_sentiments("afinn") interactively
## you might also need to edit line 49 of 10_plot_tweet_labels_over_time.R: replacing mean(score) with mean(value)
## as well as add
            ## if (the_cluster != "RightTroll") {
            ##     scores_election16$word_scores[,3] <- -scores_election16$word_scores[,3]
            ##     scores_election16$pivot_scores[,3] <- -scores_election16$pivot_scores[,3]
            ## }
## after line 219 of 03_analyze_russian_account_tweets_scaling_and_mi.R (if axes are being flipped in output)

## parrot is not included in checkpoint since it is not on CRAN
## devtools::install_github("wilryh/parrot", dependencies=TRUE)

#### ## Prep data

cat("\n\nRunning: code/01_process_russian_account_tweets.R\n\n")
source("code/01_process_russian_account_tweets.R")
## input: data from twitter

rm(list = ls())
cat("\n\nRunning: code/02_lindvill_warren_labels.R\n\n")
source("code/02_lindvill_warren_labels.R")
## input: data from LW

#### ## Analyze data

rm(list = ls())
cat("\n\nRunning: code/03_analyze_russian_account_tweets_scaling_and_mi.R\n\n")
source("code/03_analyze_russian_account_tweets_scaling_and_mi.R")
## warning: this script produces several large intermediate files
rm(list = ls())
cat("\n\nRunning: code/04_combine_account_category_scores_create_keyword_tables.R\n\n")
source("code/04_combine_account_category_scores_create_keyword_tables.R")

## prep for mturk
rm(list = ls())
cat("\n\nRunning: code/05_subset_all_russia_tweetids.R\n\n")
source("code/05_subset_all_russia_tweetids.R")
rm(list = ls())
cat("\n\nRunning: code/06_sample_tweets_for_mturk.R\n\n")
source("code/06_sample_tweets_for_mturk.R")
## then label on mturk

rm(list = ls())
cat("\n\nRunning: code/07_categorize_russian_tweets.R\n\n")
source("code/07_categorize_russian_tweets.R")
rm(list = ls())
cat("\n\nRunning: code/08_categorize_russian_tweets_for_irr.R\n\n")
source("code/08_categorize_russian_tweets_for_irr.R")

#### ## Results

## automated results
rm(list = ls())
cat("\n\nRunning: code/09_plot_tweet_scales_over_time.R\n\n")
source("code/09_plot_tweet_scales_over_time.R")
## labeled results
rm(list = ls())
cat("\n\nRunning: code/10_plot_tweet_labels_over_time.R (for glove)\n\n")
glove_embeddings <- TRUE
source("code/10_plot_tweet_labels_over_time.R")

rm(list = ls())
cat("\n\nRunning: code/10_plot_tweet_labels_over_time.R\n\n")
glove_embeddings <- FALSE
source("code/10_plot_tweet_labels_over_time.R")


## SI only
rm(list = ls())
cat("\n\nRunning: code/11_cluster_russian_accounts.R\n\n")
source("code/11_cluster_russian_accounts.R")



print(Sys.time())
